import request from '@/utils/request.js'

// 登录接口
export const loginApi = ({ mobile, code }) => request({
  url: '/v1_0/authorizations',
  method: 'post',
  data: {
    mobile,
    code
  }
})

// 获取所有频道
export const getChannelApi = () => request({
  url: '/v1_0/channels'
})

// 获取用户的频道
export const getUserChannelApi = () => request({
  url: '/v1_0/user/channels'
})

// 重置式设置用户频道
export const resetUserChannelApi = (channels) => request({
  url: '/v1_0/user/channels',
  method: 'PUT',
  data: {
    channels
  }
})

// 删除指定频道
export const delChannelApi = (id) => request({
  url: `/v1_0/user/channels/${id}`,
  method: 'DELETE'
  // headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})

// 获取文章新闻推荐
export const getArticleNewsApi = ({ channel_id, timestamp = new Date() }) => request({
  url: '/v1_0/articles',
  params: {
    channel_id,
    timestamp
  }
})

// 不感兴趣接口
export const dislikeArticleApi = (target) => request({
  url: '/v1_0/article/dislikes',
  method: 'POST',
  data: {
    target
  }
})

// 举报文章的接口
export const reportArticleApi = ({
  target, type
}) => request({
  url: '/v1_0/article/reports',
  method: 'POST',
  data: {
    target,
    type,
    remake: '就是要举报'
  }
})

// 搜索自动补全
export const suggestSearchApi = (key) => request({
  url: '/v1_0/suggestion',
  params: {
    q: key
  }
})

// 获取搜索结果
export const getSearchApi = ({
  page = 1, per_page = 10,
  q
}) => request({
  url: '/v1_0/search',
  params: {
    page,
    per_page,
    q
  }
})

// 获取文章详情
export const getArticleInfo = (id) => request({
  url: `/v1_0/articles/${id}`
})

// 关注作者
export const userFollowApi = (id) => request({
  url: '/v1_0/user/followings',
  method: 'POST',
  data: {
    target: id
  }
})

// 取关
export const userInFollowApi = (id) => request({
  url: `/v1_0/user/followings/${id}`,
  method: 'DELETE'
})

// 对文章点赞
export const attitudeApi = (id) => request({
  url: '/v1_0/article/likings',
  method: 'POST',
  data: {
    target: id
  }
})

// 取消文章点赞
export const inAttitudeApi = (id) => request({
  url: `/v1_0/article/likings/${id}`,
  method: 'DELETE'
})

// 获取评论或评论回复
export const getCommentApi = ({
  type,
  source,
  offset = null,
  limit = 10
}) => request({
  url: '/v1_0/comments',
  params: {
    type,
    source,
    offset,
    limit
  }
})

// 点赞评论
export const likeCommentApi = (target) => request({
  url: '/v1_0/comment/likings',
  method: 'POST',
  data: {
    target
  }
})

// 取消点赞评论
export const dislikeCommentApi = (target) => request({
  url: `/v1_0/comment/likings/${target}`,
  method: 'DELETE'
})

// 对文章或者评论进行评论
export const sendCommentAPI = ({ target, content, art_id = null }) => {
  const obj = { target, content }
  // 如果第三个值传参了,代表评论的是评论,需要增加文章id,否则无需增加第三个参数
  if (art_id !== null) {
    obj.art_id = art_id
  }
  return request({

    url: '/v1_0/comments',
    method: 'POST',
    data: obj
  })
}

// 获取用户资料
export const getUserInfoApi = () => request({
  url: '/v1_0/user/profile'
})
export const getUserAllInfoApi = () => request({
  url: '/v1_0/user'
})

// 编辑用户照片资料
export const setUserImageApi = (obj) => request({
  url: '/v1_0/user/photo',
  method: 'PATCH',
  // formdata对象不用写请求头,系统自带
  data: obj
})

// 编辑用户个人资料
export const setUserAnyInfoApi = (obj) => {
  // 这里可以通过外部传值直接修改这里,但需要突出语义化
  const oldobj = {
    name: null,
    gender: null,
    birthday: null,
    real_name: null,
    intro: null
  }
  // 使用assign方法
  const newObj = Object.assign(oldobj, obj)
  for (const key in newObj) {
    // 枚举遍历删除null的属性
    if (!newObj[key]) {
      delete newObj[key]
    }
  }
  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: newObj
  })
}

// 刷新token
export const resetTokenApi = () => request({
  url: '/v1_0/authorizations',
  method: 'PUT',
  headers: { Authorization: 'Bearer ' + localStorage.getItem('refresh') }
})
